package middle;

public class Solution_223 {

    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        if (ax1 <= bx1 && ax2 >= bx2 && ay1 <= by1 && ay2>=by2) {
            // a 包含 b
            return (ax2 - ax1) * (ay2 - ay1);
        } else if (bx1 <= ax1 && bx2 >= ax2 && by1 <= ay1 && by2>=ay2) {
            // b 包含 a
            return (bx2 - bx1) * (by2 - by1);
        } else if (ay1 >= by2 || by1 >= ay2 || ax2 <= bx1 || bx2 <= ax1) {
            // 相离 或 相切
            return (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1);
        } else {
            // 相交
            return (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - (Math.min(ay2, by2) - Math.max(ay1, by1)) * (Math.min(ax2, bx2) - Math.max(ax1, bx1));
        }
    }

    public static void main(String[] args) {
        Solution_223 model = new Solution_223();
        System.out.println(model.computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
        System.out.println(model.computeArea(-2, -2, 2, 2, -2, -2, 2, 2));
    }
}
